package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.ClassMapper;
import com.nsu.evaluation.pojo.Class;
import com.nsu.evaluation.pojo.ClassExample;
import com.nsu.evaluation.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    /**
     * 根据班级id获取班级名称
     * @param classId
     * @return
     */
    @Override
    public ServerResponse<String> getClassNameById(String classId){
        Class clazz = classMapper.selectByPrimaryKey(classId);
        if(clazz != null){
            return ServerResponse.createBySuccess("获取班级名称成功",clazz.getClassName());
        }
        return ServerResponse.createByErrorMessage("获取班级名称失败");
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @Override
    public ServerResponse<Class> addClass(Class clazz){
        int rowCount = classMapper.insert(clazz);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("新增班级成功",clazz);
        }
        return ServerResponse.createByErrorMessage("新增班级失败");
    }

    /**
     * 删除班级失败
     * @param classId
     * @return
     */
    @Override
    public ServerResponse deleteClass(String classId){
        int rowCount = classMapper.deleteByPrimaryKey(classId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("删除班级成功",classId);
        }
        return ServerResponse.createByErrorMessage("删除班级失败");
    }

    /**
     * 修改班级信息
     * @param clazz
     * @return
     */
    @Override
    public ServerResponse updateClassByPrimaryKey(Class clazz){
        int rowCount = classMapper.updateByPrimaryKey(clazz);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改班级信息成功",clazz);
        }
        return ServerResponse.createByErrorMessage("修改班级信息失败");
    }

    /**
     * 根据学年和班级名称查询班级信息
     * @param clazz
     * @return
     */
    @Override
    public ServerResponse<List<Class>> selectClassByExample(Class clazz){
        ClassExample classExample = new ClassExample();
        ClassExample.Criteria criteria = classExample.createCriteria();
        if(null != clazz.getClassName() && !clazz.getClassName().equals("")){
            criteria.andClassNameEqualTo(clazz.getClassName());
        }
        if(null != clazz.getClassYear() && clazz.getClassYear().equals("")){
            criteria.andClassYearEqualTo(clazz.getClassYear());
        }
        List<Class> classList = classMapper.selectByExample(classExample);
        return ServerResponse.createBySuccess("查询班级信息成功",classList);
    }

    /**
     * 获取班级列表
     * @return
     */
    @Override
    public ServerResponse<List<Class>> selectClassList(){
        ClassExample classExample = new ClassExample();
        List<Class> classList = classMapper.selectByExample(classExample);
        return ServerResponse.createBySuccess("获取班级列表成功",classList);
    }
}
